﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceTools
{
    public partial class IntellisenseTablesForm : Form
    {
        public string SelectedText { get; set; }
        private string typedString = "";
        private string strTableName = null;
        private string strTablePrefix = null;
        public IntellisenseTablesForm()
        {
            InitializeComponent();
        }

        private void OnKeyUp(object sender, KeyEventArgs e)
        {
        }

        private void m_listViewItems_KeyDown(object sender, KeyEventArgs e)
        {
            typedString += e.KeyCode.ToString();
            if (e.KeyCode == Keys.Escape)
            {
                this.SelectedText = "";
                this.Close();
            }
            
            if (e.KeyCode == Keys.Enter) 
            {

                if (this.m_listViewItems.SelectedItems.Count > 0)
                {
                    this.SelectedText = EncloseSelected(m_listViewItems.SelectedItems[0].Text);
                }
                else
                    this.SelectedText = "";
                this.Close();
            }

            if (e.KeyCode == Keys.OemPeriod)
            {
                
                if (this.m_listViewItems.SelectedItems.Count > 0)
                {
                    this.SelectedText = EncloseSelected( m_listViewItems.SelectedItems[0].Text) + ".";
                }
                else
                    this.SelectedText = "";
                this.Close();
            }
            
        }

        private string EncloseSelected(string strSel)
        {
            string strReturn = "";
            if (strSel.Contains("."))
            {
                string strFirst = strSel.Substring(0, strSel.IndexOf('.'));
                string strLast = strSel.Substring(strFirst.Length + 1);
                
                strReturn = CheckForSpaces(strFirst) + "." + CheckForSpaces(strLast);
            }
            else
            {
                strReturn = CheckForSpaces(strSel);
            }
            return strReturn;
        }

        private string CheckForSpaces(string strToCheck)
        {
            if (strToCheck.Contains(" "))
                return "[" + strToCheck + "]";
            return strToCheck;
        }

        private void OnKeypress(object sender, KeyPressEventArgs e)
        {

        }

        private void IntellisenseForm_Load(object sender, EventArgs e)
        {
            
            DBSourceToolsLib.SourceDatabase oSource = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(DBSourceToolsLib.UserSettings.Instance().CurrentDbNickName);
            SelectedText = "";
            if (string.IsNullOrEmpty(strTableName))
            {
                foreach (DBSourceToolsLib.SysObjects.SOTable oTable in oSource.SysObjects.Tables)
                {
                    ListViewItem oItem = m_listViewItems.Items.Add(oTable.Name,0);
                }
                foreach (DBSourceToolsLib.SysObjects.SOView oView in oSource.SysObjects.Views)
                {
                    ListViewItem oItem = m_listViewItems.Items.Add(oView.Name, 1);
                }
                this.m_listViewItems.Columns.RemoveAt(1);
                m_listViewItems.Columns[0].Width = 250;
            }
            else
            {
                m_listViewItems.Columns[0].Width = 175;
                m_listViewItems.Columns[1].Width = 75;
                DBSourceToolsLib.SysObjects.SOTable oTable = 
                    (DBSourceToolsLib.SysObjects.SOTable)oSource.SysObjects.getSysObject("Tables", strTableName);
                if (oTable == null)
                {
                    string strTableWithPrefix = strTablePrefix + "." + strTableName;
                    oTable =
                        (DBSourceToolsLib.SysObjects.SOTable)oSource.SysObjects.getSysObject("Tables", strTableWithPrefix);
                }

                if (oTable == null)
                {
                    // do we have a view ?
                    DBSourceToolsLib.SysObjects.SOView oView =
                        (DBSourceToolsLib.SysObjects.SOView)oSource.SysObjects.getSysObject("Views", strTableName);
                    if (oTable == null)
                    {
                        string strTableWithPrefix = strTablePrefix + "." + strTableName;
                        oView =
                            (DBSourceToolsLib.SysObjects.SOView)oSource.SysObjects.getSysObject("Views", strTableWithPrefix);
                    }
                    if (oView == null)
                    {
                        this.Close();
                        return;
                    }
                    else
                    {
                        foreach (DBSourceToolsLib.SysObjects.ColInfo oInfo in oView.Columns)
                        {
                            ListViewItem oItem = m_listViewItems.Items.Add(
                                oInfo.Name,
                                (oInfo.PrimaryKey ? 3 : 2)
                                );
                            oItem.SubItems.Add(oInfo.Type);
                        }
                    }
                }
                else
                {
                    foreach ( DBSourceToolsLib.SysObjects.ColInfo oInfo in oTable.Columns)
                    {
                        ListViewItem oItem = m_listViewItems.Items.Add(
                            oInfo.Name, 
                            (oInfo.PrimaryKey ? 3 : 2)
                            );
                        oItem.SubItems.Add(oInfo.Type);
                        
                    }
                }


            }
            
        }

        internal void SetTableName(string strPrevWord)
        {
            this.strTableName = strPrevWord;
        }

        internal void SetTablePrefix(string strPrevPrevWord)
        {
            this.strTablePrefix = strPrevPrevWord;
        }
    }
}
